package com.wc.alorithm_blue_bridge._二分.大衣的旅行;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2025/3/15 9:00
 * @description
 * https://www.lanqiao.cn/courses/46292/learning/?id=3442366&compatibility=false
 */
public class Main {
    /**
     * 遍历最大距离即可, 其中老师是可以在中间的
     *
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 10010;
    static int[][] s;
    static int n, m, k;

    public static void main(String[] args) {
        int T = sc.nextInt();
        while (T-- > 0) {
            n = sc.nextInt();
            m = sc.nextInt();
            k = sc.nextInt();
            s = new int[n + 1][m + 1];
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                    s[i][j] = sc.nextInt();
                    s[i][j] += s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1];
                }
            }
            if (s[n][m] < k + 1) out.println(-1);
            else {
                int l = 1, r = Math.max(n, m);
                while (l < r) {
                    int mid = l + r >> 1;
                    if (check(mid)) r = mid;
                    else l = mid + 1;
                }
                out.println(r - 1);
            }
        }
        out.flush();
    }

    static boolean check(int mid) {
        // 枚举老师的位置, 找到距离老师最最远的点 i +- mid
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                int v = s[i][j] - s[i][j - 1] - s[i - 1][j] + s[i - 1][j - 1];
                // 老师住不了这个地方
                if (v == 0) continue;
                int x1 = Math.max(1, i - mid + 1), x2 = Math.min(n, i + mid - 1);
                int y1 = Math.max(1, j - mid + 1), y2 = Math.min(m, j + mid - 1);
                int sum = s[x2][y2] - s[x2][y1 - 1] - s[x1 - 1][y2] + s[x1 - 1][y1 - 1];
                if (sum >= k + 1) return true;
            }
        }

        return false;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}

